package com.github;


import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangyadong
 * @Date 2021/7/30 16:00
 */
public final class MyAutoTestContext {

    public static final String PATH = File.separator + "wyd" + File.separator;
    public static final String CURRENT_PATH = File.separator + "current" + File.separator;
    public static final String BLANK = "";
    public static final String DBLANK = " ";


    private MyAutoTestContext() {
    }

    private static final Map<String, Commander> ROUTER = new HashMap<>();

    static {
        ROUTER.computeIfAbsent("inter", k -> new InterfaceCommander());
        ROUTER.computeIfAbsent("env", k -> new EnvCommander());
        ROUTER.computeIfAbsent("conf", k -> new ConfCommander());
        ROUTER.computeIfAbsent("user", k -> new UserCommander());
//        ROUTER.computeIfAbsent("token", k -> new TokenCommander());
    }


    public static void main(String[] args) {
        if (args.length == 0) {
            throw new IllegalArgumentException("启动参数不可以为空");
        }
        Map<String, String> map = new HashMap<>();
        String key = BLANK;
        Commander commander = null;
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            if (arg == null || arg.equals(BLANK)) {
                continue;
            }
            if (i == 0) {
                commander = ROUTER.get(arg);
                if (commander == null) {
                    System.out.println("not found " + arg);
                    return;
                }
            } else {
                boolean b = arg.startsWith("-");
                if (b) {
                    key = arg;
                } else {
                    map.merge(key, arg, (a, b1) -> a + DBLANK + b1);
                }
            }
        }
        commander.run(map);

    }



}
